#include<SoftwareSerial.h>

#define TRXRATE 19200
#define TERMBAUD 9600
#define LEDPIN_1 8
#define LEDPIN_2 5
#define BTNPIN 6

#define RX_1 9
#define TX_1 10

int btnstate;

SoftwareSerial mySerial = SoftwareSerial(RX_1,TX_1);

void setup() {
	Serial.begin(TERMBAUD);
	Serial.println("attempting to initialize Serial comms...");
	Serial.println("...");
	pinMode(RX_1, INPUT_PULLUP);
	pinMode(TX_1, OUTPUT);
        mySerial.begin(TRXRATE);
	pinMode(LEDPIN_1, OUTPUT);
        pinMode(LEDPIN_2, OUTPUT);
	pinMode(BTNPIN, INPUT);
	digitalWrite(LEDPIN_1, LOW);
        digitalWrite(LEDPIN_2, LOW);
	Serial.println("...initialization complete.");
}

void loop() {
	btnstate = digitalRead(BTNPIN);
	if(btnstate == HIGH) {
		mySerial.write(170);
		Serial.println("Byte sent! Waiting 5 seconds...");
                digitalWrite(LEDPIN_1, HIGH);
		delay(2500);
                digitalWrite(LEDPIN_1, LOW);
                delay(2500);
	} else {
		mySerial.listen();
		if(mySerial.peek() == char(170)) {
                        mySerial.read();
			digitalWrite(LEDPIN_2, HIGH);
			Serial.println("Byte received! Waiting 5 seconds...");
                        delay(2500);
                        digitalWrite(LEDPIN_2, LOW);
                        delay(2500);
		}
	}
}
